/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Mysql;
import Model.ResultSetTableModel;
import View.ListView;
import View.NewClient;
import java.awt.event.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jamazariegosr
 */
public class ControllerList implements KeyListener, ActionListener, MouseListener{
    
    private static ListView view;
    private static Mysql mysql;
    private static ResultSetTableModel modelTable;

    public ControllerList(ListView view,Mysql mysql) {
        this.view = view;
        this.mysql = mysql;
        
        //KeyListener         
        this.view.getFieldFind().addKeyListener(this);
        
        // datos iniciales a la tabla
        
        updateData();
        this.view.getTableList().addMouseListener(this);
        
       this.view.getButtonNew().addActionListener(this);
        
        
        
    }
    
    
    public static void updateData(){
        try {
            modelTable = new ResultSetTableModel("SELECT *  FROM employee order by barcode ",mysql);
             view.getTableList().setModel(modelTable);
           
           
        } catch (SQLException ex) {
            Logger.getLogger(ControllerList.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ControllerList.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ControllerList.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    @Override
    public void keyTyped(KeyEvent ke) {
        
        
        System.out.println("Typed : "+ke.getKeyChar());
    }

    @Override
    public void keyPressed(KeyEvent ke) {
        
    }

    @Override
    public void keyReleased(KeyEvent ke) {
       
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == view.getButtonNew()){
            
             // Nuevo  
                   
            NewClient nc = new NewClient(mysql);
            nc.setLocationRelativeTo(null);
            nc.setVisible(true);
        }
    }

    @Override
    public void mouseClicked(MouseEvent evt) {
       // si da click 
        
        if (evt.getSource() == view.getTableList()){
            int fila = view.getTableList().rowAtPoint(evt.getPoint());
                if (fila > -1)
                {
                      //dispose();
                      Object objeto = modelTable.getValueAt(fila, 0);

                     String ja = objeto.toString();
                     int num=Integer.parseInt(ja);


                      NewClient va = new NewClient (mysql,num);
                      va.setLocationRelativeTo(null);
                      va.setVisible(true);
                      

                }
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseEntered(MouseEvent me) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent me) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
    
    
    
    
    
}
